package com.li.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;

import com.li.utils.TransformUtil;

/**
 * Author: Li
 * Date: 2017/8/8
 * Description:
 */

public class BadgeImageView extends android.support.v7.widget.AppCompatImageView {
    private int mWidth;
    private Paint mOuterPaint;
    private Paint mTextPaint;
    private Paint mInnerPaint;
    //文字距离圆的边境距离
    private float mPadding = 4;
    private RectF mOuterRectF;
    private RectF mInnerRectF;

    private float mTxtLength;
    private float mTxtHeight;
    private float mStrokeWidth=5;
    public String getText() {
        return mText;
    }

    public void setText(String text) {
        mText = text;
    }

    private String mText;

    private int mBadgeNum;


    public BadgeImageView(Context context) {
        super(context);
    }

    public BadgeImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mOuterPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mOuterPaint.setColor(Color.WHITE);
        mOuterPaint.setStyle(Paint.Style.STROKE);
        mOuterPaint.setStrokeWidth(mStrokeWidth);
        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setTextSize(TransformUtil.dp2px(context,10));
        mTextPaint.setColor(Color.WHITE);

        mInnerPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        mInnerPaint.setColor(Color.RED);
        mInnerPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (getBadgeNum() <= 0) {
            return;
        }
        if (getBadgeNum() <= 99) {
            canvas.drawOval(calculateOuterOval(), mOuterPaint);
            canvas.drawOval(calculateInnerRectF(),mInnerPaint);
        } else {
            canvas.drawRoundRect(calculateOuterOval(), 40, 40, mOuterPaint);
            canvas.drawRoundRect(calculateInnerRectF(), 40, 40, mInnerPaint);
        }
        drawText(canvas, mText, mTextPaint);

    }


    /**
     * 计算文字长度以及高度
     */
    private void calculateTextSize() {

        if (getBadgeNum() > 0 && getBadgeNum() <= 99) {
            mTxtLength = mTextPaint.measureText("99");
            mTxtHeight = mTxtLength;
            mText = String.valueOf(getBadgeNum());
        } else {
            mTxtLength = mTextPaint.measureText("99+");
            mTxtHeight = mTextPaint.measureText("99");
            mText = "99+";
        }
    }


    private RectF calculateOuterOval() {
        mOuterRectF = mOuterRectF == null ? new RectF() : mOuterRectF;
        mOuterRectF.set(mWidth - mTxtLength - 2 * mPadding-mStrokeWidth/2,
                mStrokeWidth, mWidth-mStrokeWidth/2,
                mTxtHeight + 2 * mPadding+mStrokeWidth);
        return mOuterRectF;
    }
    private RectF calculateInnerRectF(){
        mInnerRectF = mInnerRectF == null ? new RectF() : mInnerRectF;
        mInnerRectF.set(mWidth - mTxtLength - 2 * mPadding-mStrokeWidth/2,
                mStrokeWidth, mWidth-mStrokeWidth/2,
                mTxtHeight + 2 * mPadding+mStrokeWidth);
        return mInnerRectF;
    }

    public int getBadgeNum() {
        return mBadgeNum;
    }

    public void setBadgeNum(int badgeNum) {
        mBadgeNum = badgeNum;
        calculateTextSize();
        invalidate();
    }

    private void drawText(Canvas canvas, String text, Paint paint) {
        float x;
        if (text.length() == 1) {
            x= calculateOuterOval().centerX()-mTxtLength/4;
//            x = mWidth - mTxtLength / 2 - mPadding - mTxtLength / 4;
        } else {
//            x = mWidth - mTxtLength - mPadding;
            x= calculateOuterOval().centerX()-mTxtLength/2;
        }

        Paint.FontMetrics metrics = paint.getFontMetrics();
        float y = Math.abs(metrics.ascent + metrics.descent) / 2 +
                (mTxtHeight + mPadding * 2) / 2+mStrokeWidth;
        canvas.drawText(text, x, y, paint);
    }
}
